package com.djx.controller;

import com.alibaba.fastjson.JSON;
import com.djx.ES.SysUser;
import com.djx.entity.User;
import com.djx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userservice;

    /**
     * 查询所有mysql数据
     * http://localhost:8188/user/findAll
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<User> findAll() {
        List<User> all = userservice.findAll();
        return all;
    }

    /**
     * 创建索引
     *
     * @return
     */
    @GetMapping("/createIndex")
    public Boolean createIndex() {
        return userservice.createIndex();
    }

    /**
     * 删除索引
     *
     * @return
     */
    @GetMapping("/deleteIndex")
    public Boolean deleteIndex() {
        return userservice.deleteIndex();
    }


    /**
     * 判断索引是否存在
     * http://localhost:8188/user/exit/user_index
     *
     * @param index
     * @return
     */
    @GetMapping("/exit/{index}")
    public String exit(@PathVariable("index") String index) {
        Boolean b = userservice.exit(index);
        if (b)
            return index + "索引存在";
        else return index + "索引不存在";
    }

    /**
     * 导入数据到ES
     * http://localhost:8188/user/import
     */
    @GetMapping("/import")
    public String importES() {
        userservice.importES();
        return "导入成功";
    }

    /**
     * 删除全部数据
     * http://localhost:8188/user/deleteAll
     */
    @GetMapping("/deleteAll")
    public String deleteAll() {
        userservice.deleteAll();
        return "删除成功";
    }

    /**
     * 新增或修改
     * http://localhost:8188/user/save
     */
    @GetMapping("/save")
    public String save() {
        userservice.save();
        return "新增/修改成功";
    }


    /**
     * 删除指定数据
     * http://localhost:8188/user/delete
     */
    @GetMapping("/delete")
    public String delete() {
        userservice.delete();
        return "删除指定条件数据成功";
    }

    /**
     * 查询全部数据  map展示
     *
     * @return
     */
    @GetMapping("/queryAll")
//    public Map<String,Object> query(){
    // 哈哈  转换了好几次 自己鼓捣玩呢
    public Object queryAll() {
        Map<String, Object> stringObjectMap = userservice.queryAll();
        Object content = stringObjectMap.get("content");
        String s = JSON.toJSONString(content);
        List<User> userList = JSON.parseArray(s, User.class);
        return userList;
    }

    /**
     * 分页 java 自带api 搜索
     * http://localhost:8188/user/queryPage/0/3/%E6%80%BC
     * @param pageNo
     * @param pageSize
     * @param keywords
     * @return
     * @throws IOException
     */
    @GetMapping("/queryPage/{pageNo}/{pageSize}/{keywords}")
    public List<Map<String, Object>> queryPage(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, @PathVariable("keywords") String keywords) throws IOException {
        return userservice.queryPage(pageNo, pageSize, keywords);
    }

    /**
     * 高亮分页搜索
     * http://localhost:8188/user/searchHeight/0/3/%E6%80%BC
     * @param pageNo
     * @param pageSize
     * @param keywords
     * @return
     * @throws IOException
     */
    @GetMapping("/searchHeight/{pageNo}/{pageSize}/{keywords}")
    public List<Map<String, Object>> searchHeight(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, @PathVariable("keywords") String keywords) throws IOException {
        return userservice.searchHeight(pageNo, pageSize, keywords);
    }



}
